package edu.cueb.wmis.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.cueb.wmis.pojo.Good;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author liuzizhuo
 * @since 2021-04-19
 */
public interface GoodMapper extends BaseMapper<Good> {

    IPage<Good> getGoodByPage(Page<Good> page, @Param("good") Good good);

    List<Good> getGoodsByShelfIds(List<Integer> shelfIds);

    @Update("UPDATE tb_good SET storeNumber = storeNumber - #{number} WHERE id = #{goodId}")
    void reduceStoreNumber(@Param("goodId") Integer goodId, @Param("number") Integer number);

    @Select("SELECT * FROM tb_good WHERE storeNumber = 0")
    List<Good> getGoodZero();

    @Select("SELECT shelfId FROM tb_good GROUP BY `shelfId` HAVING SUM(`storeNumber`)=0")
    List<Integer> getShelfZero();

    @Select("SELECT * FROM tb_good g WHERE g.`shelfId` IN (SELECT shelfId FROM tb_good GROUP BY `shelfId` HAVING SUM(`storeNumber`)=0)")
    List<Good> getNullGoods();
    @Select("SELECT g.`shelfId` FROM tb_good g GROUP BY g.`shelfId` HAVING SUM(g.`storeNumber`)=0")
    List<Integer> getEmptyShelf();


    @Select("SELECT g.* ,h.`name` AS hname,s.`name` AS sname FROM  tb_good g LEFT JOIN tb_shelf s ON g.`shelfId` = s.`id` INNER JOIN tb_house_shelf hs ON hs.`sid` = s.`id`LEFT JOIN tb_house h ON h.id = hs.`hid` WHERE g.id = #{goodId}")
    @ResultMap("GoodInfo")
    Good getGoodInfo(Integer goodId);
}
